﻿#include <Windows.h>
#include <tchar.h>
#include <list>
using namespace std;
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Error.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_RfcComponents/RfcComponents_HttpServer/HttpServer_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_RfcComponents/RfcComponents_HttpServer/HttpServer_Error.h"

#pragma comment(lib,"Ws2_32.lib")
#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/NetEngine_Core.lib")
#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/RfcComponents_HttpServer.lib")

XNETHANDLE xhToken;
BOOL __stdcall NetCore_CB_Login(LPCSTR lpszClientAddr, SOCKET hSocket, LPVOID lParam)
{
	printf("NetCore_CB_Login:%s\n", lpszClientAddr);
	RfcComponents_HttpServer_CreateClient(lpszClientAddr);
	return TRUE;
}
void __stdcall NetCore_CB_Recv(LPCSTR lpszClientAddr, SOCKET hSocket, LPCSTR lpszRecvMsg, int nMsgLen, LPVOID lParam)
{
	printf("NetCore_CB_Recv:%s-%d\n", lpszClientAddr, nMsgLen);
	if (!RfcComponents_HttpServer_InserQueue(lpszClientAddr, lpszRecvMsg, nMsgLen))
	{
		printf("RfcComponents_WSPacket_Post:%lX\n", HttpServer_GetLastError());
	}
}
void __stdcall NetCore_CB_Close(LPCSTR lpszClientAddr, SOCKET hSocket, LPVOID lParam)
{
	printf("NetCore_CB_Close:%s\n", lpszClientAddr);
	RfcComponents_HttpServer_CloseClinet(lpszClientAddr);
}

DWORD WINAPI NetCore_Thread(LPVOID lParam)
{
	while (TRUE)
	{
		if (RfcComponents_HttpServer_EventWait())
		{
			int nMsgLen = 20480;
			RFCCOMPONENTS_HTTP_REQPARAM st_ReqParam;
			TCHAR tszMsgBuffer[20480];
			TCHAR tszClientAddr[64];
			list<tstring> stl_ListHdrField;

			memset(&st_ReqParam, '\0', sizeof(RFCCOMPONENTS_HTTP_REQPARAM));
			memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
			memset(tszClientAddr, '\0', sizeof(tszClientAddr));

			if (RfcComponents_HttpServer_GetRandom(tszClientAddr, &stl_ListHdrField, &st_ReqParam, tszMsgBuffer, &nMsgLen))
			{
				printf("%s %d:%s\n", tszClientAddr, nMsgLen, tszMsgBuffer);
				RFCCOMPONENTS_HTTP_HDRPARAM st_HdrParam;
				memset(&st_HdrParam, '\0', sizeof(RFCCOMPONENTS_HTTP_HDRPARAM));

				st_HdrParam.bIsClose = TRUE;
				st_HdrParam.nHttpCode = 200;
				RfcComponents_HttpServer_SendMsg(tszMsgBuffer, &nMsgLen, &st_HdrParam);
				NetCore_TCPIocp_SendEx(xhToken, tszClientAddr, tszMsgBuffer, nMsgLen);
			}
		}
		Sleep(1);
	}
	return 0;
}

int main()
{
	WSADATA st_WSAData;
	WSAStartup(MAKEWORD(2, 2), &st_WSAData);

	LPCTSTR lpszMiniFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\HttpMime.types");
	LPCTSTR lpszCodeFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\HttpCode.types");

	if (!RfcComponents_HttpServer_Init(lpszCodeFile,lpszMiniFile))
	{
		printf("%lX\n", HttpServer_GetLastError());
		return 0;
	}
	if (!NetCore_TCPIocp_StartEx(&xhToken))
	{
		printf("%lX\n", NetCore_GetLastError());
		return 0;
	}

	NetCore_TCPIocp_RegisterCallBackEx(xhToken, NetCore_CB_Login, NetCore_CB_Recv, NetCore_CB_Close);
	CreateThread(NULL, 0, NetCore_Thread, NULL, 0, NULL);
	while (1)
	{
		Sleep(1000);
	}
	NetCore_TCPIocp_DestroyEx(xhToken);
	RfcComponents_HttpServer_Destroy();
	WSACleanup();
	return 0;
}